import { defineStore } from "pinia";
import { listDepartmentByCondition } from "@/api/data-dictionary/department";

export interface DepartmentSearchCondition {
  departmentNo?: string;
  departmentName?: string;
}

export interface Department {
  id?: number | null;
  departmentNo?: string | null;
  departmentName?: string | null;
}

export interface DepartmentPage {
  departmentList: Department[];
  departmentInsertDialogVisible?: boolean;
  departmentEditDialogVisible?: boolean;
  departmentSearchDialogVisible?: boolean;
  departmentSelectedRow?: Department;
}

export const useDepartmentStore = defineStore({
  id: "app-department",
  state: (): DepartmentPage => ({
    departmentList: [],
    departmentInsertDialogVisible: false,
    departmentEditDialogVisible: false,
    departmentSearchDialogVisible: false,
    departmentSelectedRow: {} as Department,
  }),
  getters: {
    getDepartmentList(): Department[] {
      return this.departmentList;
    },
    getDepartmentInsertDialogVisible(): boolean {
      return this.departmentInsertDialogVisible as boolean;
    },
    getDepartmentEditDialogVisible(): boolean {
      return this.departmentEditDialogVisible as boolean;
    },
    getDepartmentSearchDialogVisible(): boolean {
      return this.departmentSearchDialogVisible as boolean;
    },
    getDepartmentSelectedRow(): Department {
      return this.departmentSelectedRow as Department;
    },
  },
  actions: {
    async setDepartmentList(condition: Department) {
      try {
        const response = await listDepartmentByCondition(condition);
        console.log(response);
        this.departmentList = response;
      } catch (e) {
        return Promise.reject(e);
      }
    },
    openDepartmentInsertDialog() {
      this.departmentInsertDialogVisible = true;
    },
    closeDepartmentInsertDialog() {
      this.departmentInsertDialogVisible = false;
    },
    openDepartmentEditDialog() {
      this.departmentEditDialogVisible = true;
    },
    closeDepartmentEditDialog() {
      this.departmentEditDialogVisible = false;
    },
    openDepartmentSearchDialog() {
      this.departmentSearchDialogVisible = true;
    },
    closeDepartmentSearchDialog() {
      this.departmentSearchDialogVisible = false;
    },
    setDepartmentSelectedRow(row: Department) {
      this.departmentSelectedRow = JSON.parse(
        JSON.stringify(row)
      ) as Department;
    },
  },
});
